package com.tuozixuan.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;

public class DemoFilter extends ZuulFilter {
    
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 是否執行該過濾器，true為執行，false為不執行
     */
    public boolean shouldFilter() {
        RequestContext requestContext = RequestContext.getCurrentContext();
        Object isSuccess = requestContext.get("isSuccess");
        return isSuccess == null ? true : Boolean.parseBoolean(isSuccess.toString());
    }

    /**
     * 執行自己的業務邏輯
     */
    public Object run() throws ZuulException {
        logger.info("DemoFilter run start...");
        RequestContext requestContext = RequestContext.getCurrentContext();
        String ip = (String)requestContext.get("ip");
        logger.info("ip:" + ip);
        logger.info("DemoFilter run end...");
        return null;
    }

    /**
     * 過濾器類型，可選值為pre、route、post、error
     */
    @Override
    public String filterType() {
        return "pre";
    }

    /**
     * 過濾器的執行順序，數值越小，優先級越高
     */
    @Override
    public int filterOrder() {
        return 2;
    }
    
}
